package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.common.utils.PageUtils;
import com.atguigu.spzx.manager.entity.ProductSpec;
import com.atguigu.spzx.manager.mapper.ProductSpecMapper;
import com.atguigu.spzx.manager.service.ProductSpecService;
import com.atguigu.spzx.manager.vo.productSpec.ProductSpecQueryRespVo;
import com.atguigu.spzx.manager.vo.productSpec.ProductSpecSaveReqVo;
import com.atguigu.spzx.manager.vo.productSpec.ProductSpecUpdateReqVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * ProductSpecServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/10/30
 */
@Service
public class ProductSpecServiceImpl implements ProductSpecService {

    @Autowired
    private ProductSpecMapper productSpecMapper;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public Page<ProductSpecQueryRespVo> getProductSpecPage(Page<ProductSpec> pageParam) {
        Page<ProductSpec> page = productSpecMapper.selectPage(pageParam,null);
        return PageUtils.convertTo(page,ProductSpecQueryRespVo.class);
    }

    /**
     * 添加
     * @param reqVo
     * @return
     */
    @Override
    public void saveProductSpec(ProductSpecSaveReqVo reqVo) {
        ProductSpec productSpec = BeanUtils.toBean(reqVo, ProductSpec.class);
        productSpecMapper.insert(productSpec);
    }

    /**
     * 修改
     * @param reqVo
     * @return
     */
    @Override
    public void updateProductSpecById(ProductSpecUpdateReqVo reqVo) {
        ProductSpec productSpec = BeanUtils.toBean(reqVo, ProductSpec.class);
        productSpecMapper.updateById(productSpec);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public void deleteProductSpecById(Long id) {
        productSpecMapper.deleteById(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public ProductSpecQueryRespVo getProductSpecById(Long id) {
        ProductSpec productSpec = productSpecMapper.selectById(id);
        return BeanUtils.toBean(productSpec, ProductSpecQueryRespVo.class);
    }

    /**
     * 查询所有商品规格
     * @return
     */
    @Override
    public List<ProductSpecQueryRespVo> getProductSpecList() {
        List<ProductSpec> list = productSpecMapper.selectList(null);
        return BeanUtils.toBeanList(list, ProductSpecQueryRespVo.class);
    }
}
